package com.wbao.dianniu.im;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.conversation.IYWConversationService;
import com.alibaba.mobileim.conversation.YWConversation;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.KnowledgeData;
import com.wbao.dianniu.data.NoticeKnowData;
import com.wbao.dianniu.listener.INoticeKnowListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.NoticeKnowListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 知识分享通知
 * <p>
 * Created by yejianjun on 2017/9/22.
 */

public class KnowNoticeHelperActivity extends BaseActivity implements INoticeKnowListListener {

    private PullToRefreshListView pullListView;
    private ListView mListView;
    private NoticeKnowListManager manager;
    private MyAdapter adapter;
    private List<NoticeKnowData> mList;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_notice_qa_helper, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.knowledge_notify));
        init();
        requestData(currentPage);
        YWIMKit iMkit = LoginSampleHelper.getInstance().getIMKit();
        IYWConversationService conversationService = iMkit.getConversationService();
        YWConversation conversation = conversationService.getConversationByUserId("121", LoginSampleHelper.APP_KEY);
        if (null != conversation)
            conversationService.markReaded(conversation);

    }

    private void init() {
        pullListView = (PullToRefreshListView) findViewById(R.id.notice_qa_listview);
        pullListView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);

        adapter = new MyAdapter(this);
        mList = new ArrayList<>();
        adapter.addData(mList);
        mListView.setAdapter(adapter);
    }

    PullToRefreshBase.OnRefreshListener onRefreshListener = new PullToRefreshBase.OnRefreshListener() {

        @Override
        public void onRefresh() {
            //  上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        //没有搜索结果 或者数据为空 上拉没有效果
        currentPage++;
        requestData(currentPage);
    }

    private void requestData(int page) {
        manager = new NoticeKnowListManager(this);
        manager.addNoticeKnowListListener(this);
        manager.noticeKnowList(GlobalContext.getAccountId(), page * PAGE_COUNTS, PAGE_COUNTS);
    }


    @Override
    public void onNoticeKnowListFailure(int errorCode, String errorMsg) {
        // TODO Auto-generated method stub
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onNoticeKnowListSuccess(List<NoticeKnowData> list) {
        pullListView.onRefreshComplete(false, false);
        if (null == list)
            return;
        adapter.addData(list);
        mList = adapter.getList();
        adapter.notifyDataSetChanged();
    }

    class MyAdapter extends BaseAdapter {

        private Context mContext;
        private List<NoticeKnowData> list;
        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
            if (null == list)
                list = new ArrayList<NoticeKnowData>();
        }

        public void addData(List<NoticeKnowData> addList) {
            for (int i = 0; i < addList.size(); i++) {
                list.add(addList.get(i));
            }
        }

        public List<NoticeKnowData> getList() {
            return list;
        }

        @Override
        public int getCount() {
            if (null == list)
                return 0;
            else
                return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.know_notify_item, null);
                holder = new Holder();
                holder.createDateTV = (TextView) convertView.findViewById(R.id.know_notify_time);
                holder.subjectTV = (TextView) convertView.findViewById(R.id.know_notify_subject);
                holder.contentTV = (TextView) convertView.findViewById(R.id.know_notify_content);
                holder.detailTV = (TextView) convertView.findViewById(R.id.know_notify_detail);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            NoticeKnowData data = list.get(position);
            holder.createDateTV.setText(Utils.formatNotifyDate(data.getCreateDate()));
            holder.subjectTV.setText(data.getSubject() == null ? "" : data.getSubject());
            holder.contentTV.setText(data.getContent() == null ? "" : data.getContent());
            holder.detailTV.setOnClickListener(new MyAdapter.MyClickListener(data));
            return convertView;
        }

        class Holder {
            public TextView createDateTV;
            public TextView subjectTV;
            public TextView contentTV;
            public TextView detailTV;
        }

        private class MyClickListener implements View.OnClickListener {

            private NoticeKnowData data;

            public MyClickListener(NoticeKnowData data) {
                this.data = data;
            }

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.know_notify_detail:
                        KnowledgeData knowledgeData = new KnowledgeData();
                        knowledgeData.setId(data.getKnowledgeId());
                        Utils.intoKnowDetailActivity(mContext, 1, knowledgeData);
                        break;

                    default:
                        break;
                }
            }

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeNoticeKnowListListener(this);
        }
    }
}
